﻿#region changelog
/*
 * 02/04/09 - wbw (Seagull Consulting): Original code
 * 02/14/09 - wbw : Remove the pipe("|") symbols in the Genre data and format like VMC (comma seperated).
 * 02/16/09 - wbw: Added extra check for reserved XML characters in the overview that might cause problems down the line.
 * 06/13/09 - wbw: Modified the code to perform a better end of XML tag search, if not an entirely correct search (i.e. it's a hack).
 */
#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Durrant.Plugins.TvDB
{
    public class Series
    {
        public string fileName { get; set; }
        public string Id { get; set; }
        public string DayOfWeek { get; set; }
        public string AirTime { get; set; }
        public string ContentRating { get; set; }
        public string Genre { get; set; }
        public string Network { get; set; }
        public string RunTime { get; set; }
        public string Overview { get; set; }
        public string SeriesName { get; set; }

        public string Name
        {
            get { return fileName.Replace(".", " "); }
        }

        public string getURLName()
        {
            string urlName = fileName;
            urlName = urlName.Replace(".", Constants.URL_SPACE);
            return urlName;
        }

        public void parse(string seriesXML)
        {
            //Parse the id
            SeriesName = removeXMLReservedCharacters(getXMLTagValue(seriesXML, Constants.SERIES_NAME));

            //Parse the id
            Id = getXMLTagValue(seriesXML, Constants.SERIES_ID);

            //Parse the DayOfWeek
            DayOfWeek = getXMLTagValue(seriesXML, Constants.SERIES_DAY_OF_WEEK);

            //Parse the AirsTime
            AirTime = getXMLTagValue(seriesXML, Constants.SERIES_AIR_TIME);

            //Parse the Content Rating
            ContentRating = getXMLTagValue(seriesXML, Constants.SERIES_CONTENT_RATING);

            //Parse the Genre
            string rawGenre = getXMLTagValue(seriesXML, Constants.SERIES_GENRE);
            if (rawGenre.Length > 2)
            {
                //strip off the leading and ending pipe "|" symbols
                rawGenre = rawGenre.Substring(1, rawGenre.Length - 2);
                //replace remaining seperator symbols with commas
            }
            Genre = rawGenre.Replace(Constants.GENRE_SEPERATOR, ",");

            //Parse the Network
            Network = getXMLTagValue(seriesXML, Constants.SERIES_NETWORK);

            //Parse the Runtime
            RunTime = getXMLTagValue(seriesXML, Constants.SERIES_RUNTIME);

            //Parse the Overview
            Overview = removeXMLReservedCharacters(getXMLTagValue(seriesXML, Constants.SERIES_OVERVIEW));

        }

        private string getXMLTagValue(string XML, string tag)
        {
            int beginLoc, endLoc = 0;
            beginLoc = XML.IndexOf(tag) + tag.Length + 1;
            if (beginLoc > 0)
            {
                //Fixed by wbw on 6/13/09 for error when idiots include the tag keyword as data between the xml tag markers
                //Example: <Network> USA Networks </Network>
                //Now performs a better, if not perfect, end of tag search
                endLoc = XML.IndexOf('/' + tag, beginLoc) - 5;
                if (endLoc > 0)
                    return XML.Substring(beginLoc, endLoc - beginLoc);
                else
                    return string.Empty;
            }
            else
                return string.Empty;
        }

        private string removeXMLReservedCharacters(string xmlString)
        {
            StringBuilder returnString = new StringBuilder(xmlString);

            //Change XML reserved characters that may give us trouble later on
            returnString = returnString.Replace(Constants.XMLRESERVEDCHARAMPERSAND, Constants.XML_RESERVED_STRING_AMPERSAND);
            returnString = returnString.Replace(Constants.XML_RESERVERED_CHAR_PERCENT, Constants.XML_RESERVED_STRING_PERCENT);
            returnString = returnString.Replace(Constants.XML_RESERVED_CHAR_GT, string.Empty);
            returnString = returnString.Replace(Constants.XML_RESERVED_CHAR_LT, string.Empty);

            return returnString.ToString();
        }
    }
}
